package org.octgame.resources.lua;

import com.naef.jnlua.LuaState;
import java.io.FileInputStream;
import java.io.IOException;



/**
 *
 * @author Hempa
 */
public class LoadScript {
    LuaState luaState;

    public LoadScript(final String fileName) throws IOException {
        this.luaState = new LuaState();

        FileInputStream file = new FileInputStream(fileName);
        luaState.load(file, fileName);
        luaState.openLibs();
        luaState.call(0,0);

    }

    public void closeScript() {
        this.luaState.close();
    }

    public void runScriptFunction(String functionName, Object obj)
    {
        this.luaState.getGlobal(functionName);
        this.luaState.pushJavaObject(obj);
        this.luaState.call(1, 0);
    }

    public void doubleReturn(String functionName)
    {
        System.out.println("Stack size:" + luaState.getTop());
        this.luaState.getGlobal(functionName);
        this.luaState.call(0,2);
        System.out.println("Stack size:" + luaState.getTop());
        String s = this.luaState.toString(1);
        int i = this.luaState.toInteger(2);
        luaState.pop(2); // Pop the last few from the stack
        System.out.println("Stack size:" + luaState.getTop());
        System.out.println("S: " + s + " i: " + i);
    }
}
